這篇的上一篇:https://ithelp.ithome.com.tw/articles/10283923
~又是JSON~~要把JSON練熟~
https://zh.wikipedia.org/wiki/JSON
這裡要用logcat取出JSON的方法:
先建立新的Android檔-
用logcat取出JSON的方法:
package com.huang.json2;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String json = String.format("{\"%s\":\"%s\"}","firstName","Ken");
Log.d("Howard","Json"+json);
}
}
使用Android內建套件
把錯誤"印"出來~
目前變這樣
然後再來改內容
package com.huang.json2;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import org.json.JSONException;
import org.json.JSONObject;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String json = String.format("{\"%s\":\"%s\"}","firstName","Ken");
Log.d("Howard","Json"+json);
try {
JSONObject obj =new JSONObject(json);
} catch (JSONException e) {
Log.e("Howard","JSONException"+e);
}
}
}
再來取出數字:
跑一下取出KEN
package com.huang.json2;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import org.json.JSONException;
import org.json.JSONObject;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String json = String.format("{\"%s\":\"%s\"}","firstName","Ken");
Log.d("Howard","Json"+json);
try {
JSONObject obj =new JSONObject(json);
String firstName = obj.getString("firstName");
Log.d("Howard","firstName:"+firstName);
} catch (JSONException e) {
Log.e("Howard","JSONException"+e);
}
}
}
動態增加按鈕不是用XML
開新檔案-
XML檔先改成-LinearLayout
XML裡面只有放...
命名- containerView
然後用JAVA檔-然後用JAVA檔-
package com.huang.dynamic_ui;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.LinearLayout;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout layout=findViewById(R.id.containerView);
Button button = new Button(this);
button.setText("存檔");
layout.addView(button);
}
}
就可以長出這樣~
然後再加入for迴圈
package com.huang.dynamic_ui;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.LinearLayout;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout layout=findViewById(R.id.containerView);
Button button = new Button(this);
button.setText("存檔");
layout.addView(button);
for(int i=1;i <=10;i ++){
CheckBox checkBox = new CheckBox(this);
checkBox.setText("Item:" + i);
layout.addView(checkBox);
}
}
}
就長成這樣:
這篇的下一篇:https://ithelp.ithome.com.tw/articles/10283962